﻿using System;
using System.Windows.Forms;

using Microsoft.Win32;

namespace WindowsApplication2._911_系统.注册表.基础
{
    public partial class Form_regedit : Form
    {
        public Form_regedit()
        {
            InitializeComponent();
        }

        private RegistryKey get_key(string str_key, RegistryKey rootkey)
        {
            if (str_key.IndexOf("\\") != -1)
            {
                string root = str_key.Substring(0, str_key.IndexOf("\\"));
                string key = str_key.Substring(str_key.IndexOf("\\") + 1, str_key.Length - str_key.IndexOf("\\") - 1);
                RegistryKey rkey1 = rootkey.OpenSubKey(root, true);

                return get_key(key, rkey1);
            }

            else
            {
                return rootkey;
            }
        }

        private void button_read_Click(object sender, EventArgs e)
        {
            try
            {
                string str_key = this.textBox_key.Text;
                string root = str_key.Substring(0, str_key.IndexOf("\\"));
                string key = str_key.Substring(str_key.IndexOf("\\") + 1, str_key.Length - str_key.IndexOf("\\") - 1);
                string key_value = key.Substring(key.LastIndexOf("\\") + 1, key.Length - key.LastIndexOf("\\") - 1);

                RegistryKey rkey = null;

                if (root.ToUpper() == "HKEY_LOCAL_MACHINE")
                {
                    rkey = Registry.LocalMachine;
                }

                RegistryKey lastkey = get_key(key, rkey);

                this.textBox_value.Text = lastkey.GetValue(key_value).ToString();

                //Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Classes\TypeLib\{F5078F18-C551-11D3-89B9-0000F81FE221}\5.0\0\win32","1","1");

                //RegistryKey rkey = Registry.LocalMachine;

                //RegistryKey rkey1 = rkey.OpenSubKey("Software", true);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }

        private void button_edit_Click(object sender, EventArgs e)
        {
            string str_key = this.textBox_key.Text;
            string root = str_key.Substring(0, str_key.IndexOf("\\"));
            string key = str_key.Substring(str_key.IndexOf("\\") + 1, str_key.Length - str_key.IndexOf("\\") - 1);
            string key_value = key.Substring(key.LastIndexOf("\\") + 1, key.Length - key.LastIndexOf("\\") - 1);

            string newvalue = this.textBox_newvalue.Text;

            RegistryKey rkey = null;

            if (root.ToUpper() == "HKEY_LOCAL_MACHINE")
            {
                rkey = Registry.LocalMachine;
            }

            RegistryKey lastkey = get_key(key, rkey);

            lastkey.SetValue(key_value, newvalue);

            MessageBox.Show("ok");
        }

        private void Form_regedit_Load(object sender, EventArgs e)
        {

        }
    }
}